﻿using System.Windows;
#if WPF
using System.Windows.Controls;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;
using Windows.Foundation;
#endif

namespace Eternal.DataVisualization
{
    public class WrapPanel : Panel
    {

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(WrapPanel), new PropertyMetadata(Orientation.Horizontal));



        protected override Size ArrangeOverride(Size finalSize)
        {
            double currWidth = 0, currHeight = 0, lineHeight = 0, lineWidth = 0;

            var columnWidth = finalSize.Width;
            var rowHeight = finalSize.Height;

            foreach (UIElement child in Children)
            {
                if (Orientation == (Orientation.Vertical))
                {

                    if (currHeight + child.DesiredSize.Height > finalSize.Height)
                    {
                        currWidth += lineWidth;
                        currHeight = 0;
                        lineWidth = 0;
                    }

                    child.Arrange(new Rect(currWidth, currHeight, child.DesiredSize.Width, child.DesiredSize.Height));

                    currHeight += child.DesiredSize.Height;
                    if (child.DesiredSize.Width > lineWidth)
                        lineWidth = child.DesiredSize.Width;
                }
                else
                {
                    if (currWidth + child.DesiredSize.Width > finalSize.Width)
                    {
                        currHeight += lineHeight;
                        currWidth = 0;
                        lineHeight = 0;
                    }

                    child.Arrange(new Rect(currWidth, currHeight, child.DesiredSize.Width, child.DesiredSize.Height));

                    currWidth += child.DesiredSize.Width;

                    if (child.DesiredSize.Height > lineHeight)
                        lineHeight = child.DesiredSize.Height;
                }
            }
            return finalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            double currWidth = 0, currHeight = 0, LineHeight = 0, LineWidth = 0;
            if (Orientation == (Orientation.Vertical))
            {
                foreach (UIElement child in Children)
                {
                    child.Measure(availableSize);
                    if (currHeight + child.DesiredSize.Height > availableSize.Height)
                    {
                        currWidth += LineWidth;
                        currHeight = 0;
                        LineWidth = 0;
                    }
                    currHeight += child.DesiredSize.Height;
                    if (child.DesiredSize.Width > LineWidth)
                        LineWidth = child.DesiredSize.Width;
                }
                currHeight += LineWidth;
            }
            else
            {
                foreach (UIElement child in Children)
                {
                    child.Measure(availableSize);

                    if (currWidth + child.DesiredSize.Width > availableSize.Width)
                    {
                        currHeight += LineHeight;
                        currWidth = 0;
                        LineHeight = 0;
                    }

                    currWidth += child.DesiredSize.Width;

                    if (child.DesiredSize.Height > LineHeight)
                        LineHeight = child.DesiredSize.Height;

                }
                currHeight += LineHeight;
            }

            return new Size(currWidth, currHeight);
        }
    }
}